import Vue from 'vue'
import Router from 'vue-router'
import store from '@/store'

Vue.use(Router)

let router = new Router({
  routes: [
    {
      path: '/',
      name: 'home',
      meta: {
        title: '首页'
      },
      component: () => import('@/components/pc')
    }, {
      path: '/login',
      name: 'login',
      meta: {
        title: '登录'
      },
      component: () => import('@/components/pc/login')
    }, {
      path: '/register',
      name: 'register',
      meta: {
        title: '注册'
      },
      component: () => import('@/components/pc/register')
    }, {
      path: '/retrievepwd',
      name: 'retrievepwd',
      meta: {
        title: '找回密码'
      },
      component: () => import('@/components/pc/retrievepwd')
    }, {
      path: '/project',
      name: 'project',
      meta: {
        title: '产品服务'
      },
      component: () => import('@/components/pc/project')
    }, {
      path: '/case',
      name: 'case',
      meta: {
        title: '项目案例'
      },
      component: () => import('@/components/pc/case')
    }, {
      path: '/team',
      name: 'team',
      meta: {
        title: '团队服务'
      },
      component: () => import('@/components/pc/team')
    }, {
      path: '/company',
      name: 'company',
      meta: {
        title: '公司动态'
      },
      component: () => import('@/components/pc/company')
    }, {
      path: '/about',
      name: 'about',
      meta: {
        title: '关于我们'
      },
      component: () => import('@/components/pc/about')
    },
    //用户页面
    {
      path: '/user',
      name: 'user',
      meta: {
        title: '个人中心',
        requireAuth: true//验证登录
      },
      component: () => import('@/components/pc/user'),
      children: [

      ]
    }
  ]
})

/**
 * 判断是否为移动设备，是，则跳转到移动端的路径
 */
router.beforeEach((to, from, next) => {
  if (/Android|webOS|iPhone|iPod|BlackBerry/i.test(navigator.userAgent)) {
    window.location.href = '/m_index.html#/'
    return
  }
  else {
    // 设置页面标题
    document.title = to.meta.title
    if (to.matched.some(c => c.meta.requireAuth)) {
      if (store.state.token) {
        store.dispatch("getInfo")
        next()
      }
      else {
        next({
          path: '/login',
          query: {
            redirect: to.fullPath
          }
        })
      }

    } else {
      next()
    }
  }
})

export default router
